package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.controller.booth.model.AddBoothModel;
import com.ls.controller.booth.model.EditBoothModel;
import com.ls.controller.booth.model.EditIndexModel;
import com.ls.entity.*;
import com.ls.mapper.BoothMapper;
import com.ls.service.IBookingService;
import com.ls.service.IBoothService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.ls.constant.SystemConstants.VALID_FALSE;
import static com.ls.constant.SystemConstants.VALID_TRUE;

/**
 * <p>
 * 卡座
 * </p>
 *
 * @author pengyu
 * @since 2024-10-12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BoothServiceImpl extends ServiceImpl<BoothMapper, Booth> implements IBoothService {
    private final IBookingService bookingService;
    /**
     * 新增卡座
     */
    @Override
    public ResultData add(AddBoothModel model) {
        //判断当前用户是否存在重复的卡座
        Booth booth1 = getOne(new LambdaQueryWrapper<Booth>()
                .eq(Booth::getSeatingId, model.getSeatingId())
                .eq(Booth::getValidFlag, VALID_TRUE)
                .eq(Booth::getBoothName, model.getBoothName()));
        if (booth1 != null) {
            return ResultData.failure("当前用户已存在该卡座！");
        }
        Booth booth = new Booth();
        BeanUtils.copyProperties(model, booth);
        booth.setCreateTime(LocalDateTime.now());
        booth.setValidFlag(VALID_TRUE);
        Boolean flag = saveOrUpdate(booth);
        if (flag) {
            return ResultData.successData(booth);
        } else {
            return ResultData.failure("新增失败");
        }
    }
    /**
     * 编辑卡座
     */
    @Override
    public ResultData edit(EditBoothModel model) {
        //判断当前用户是否存在重复的分类
        Booth booth1 = getOne(new LambdaQueryWrapper<Booth>()
                .eq(Booth::getSeatingId, model.getSeatingId())
                .eq(Booth::getValidFlag, VALID_TRUE)
                .eq(Booth::getBoothName, model.getBoothName())
                .ne(Booth::getBoothId, model.getBoothId()));
        if (booth1 != null) {
            return ResultData.failure("当前用户已存在该卡座！");
        }
        Booth booth = getById(model.getBoothId());
        BeanUtils.copyProperties(model, booth);
        Boolean flag = saveOrUpdate(booth);
        if (flag) {
            return ResultData.successMsg("更新成功");
        } else {
            return ResultData.failure("更新失败");
        }
    }
    /**
     * 删除卡座
     */
    @Override
    public ResultData del(Long boothId) {
        Booth booth = getById(boothId);
        if (booth == null){
            return ResultData.failure("该卡座不存在");
        }
        booth.setValidFlag(VALID_FALSE);
        Integer bookings=bookingService.count(new LambdaQueryWrapper<Booking>()
                .eq(Booking::getBoothId,boothId)
                .ge(Booking::getBookingDay, LocalDate.now()));
        if (bookings>0){
            return ResultData.failure("该卡座有预约，请先取消预约");
        }
        Boolean flag = saveOrUpdate(booth);
        if (flag) {
            return ResultData.successMsg("删除成功");
        } else {
            return ResultData.failure("删除失败");
        }
    }
    /**通过id查询*/
    @Override
    public ResultData getBoothById(Long boothId) {
        Booth booth = getById(boothId);
        return ResultData.successData(booth);
    }
    /**编辑卡座排序*/
    @Override
    public ResultData editIndex(EditIndexModel model) {
        for (EditIndexModel.info info : model.getList()) {
            Booth booth = getById(info.getBoothId());
            booth.setBoothIndex(info.getBoothIndex());
            saveOrUpdate(booth);
        }
        return ResultData.successMsg("排序更新成功");
    }
}
